class RMBConverter {
    static digitMap = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    static unitMap = ['', '拾', '佰', '仟'];
    static bigUnitMap = ['', '万', '亿', '万亿'];
    static fractionMap = ['角', '分'];

    static toUppercase(amount) {
        try {
            if (amount === '' || amount === null || amount === undefined) {
                return { success: false, error: '请输入金额' };
            }

            const num = parseFloat(amount);
            if (isNaN(num)) {
                return { success: false, error: '请输入有效的数字' };
            }

            if (num < 0) {
                return { success: false, error: '金额不能为负数' };
            }

            if (num >= 1000000000000) {
                return { success: false, error: '金额过大，最大支持万亿级别' };
            }

            if (num === 0) {
                return { success: true, data: '零元整' };
            }

            const result = this.convertToUppercase(num);
            return { success: true, data: result };
        } catch (error) {
            return { success: false, error: '转换失败：' + error.message };
        }
    }

    static convertToUppercase(num) {
        const numStr = num.toFixed(2);
        const [integerPart, decimalPart] = numStr.split('.');
        
        let result = '';
        
        // 处理整数部分
        if (parseInt(integerPart) > 0) {
            result += this.convertIntegerPart(integerPart) + '元';
        } else {
            result += '零元';
        }
        
        // 处理小数部分
        const jiao = parseInt(decimalPart[0]);
        const fen = parseInt(decimalPart[1]);
        
        if (jiao === 0 && fen === 0) {
            result += '整';
        } else {
            if (jiao > 0) {
                result += this.digitMap[jiao] + '角';
            } else if (fen > 0) {
                result += '零';
            }
            
            if (fen > 0) {
                result += this.digitMap[fen] + '分';
            }
        }
        
        return result;
    }

    static convertIntegerPart(integerStr) {
        const len = integerStr.length;
        let result = '';
        let zeroFlag = false;
        
        for (let i = 0; i < len; i++) {
            const digit = parseInt(integerStr[i]);
            const position = len - i - 1;
            const unitIndex = position % 4;
            const bigUnitIndex = Math.floor(position / 4);
            
            if (digit === 0) {
                zeroFlag = true;
            } else {
                if (zeroFlag && result !== '') {
                    result += '零';
                }
                result += this.digitMap[digit];
                if (unitIndex > 0) {
                    result += this.unitMap[unitIndex];
                }
                zeroFlag = false;
            }
            
            // 添加大单位（万、亿等）
            if (position > 0 && position % 4 === 0 && bigUnitIndex < this.bigUnitMap.length) {
                if (this.hasNonZeroInGroup(integerStr, len - position, 4)) {
                    result += this.bigUnitMap[bigUnitIndex];
                }
            }
        }
        
        return result;
    }

    static hasNonZeroInGroup(str, start, length) {
        for (let i = start; i < start + length && i < str.length; i++) {
            if (str[i] !== '0') {
                return true;
            }
        }
        return false;
    }

    static toLowercase(upperAmount) {
        try {
            if (!upperAmount || upperAmount.trim() === '') {
                return { success: false, error: '请输入大写金额' };
            }

            const cleanAmount = upperAmount.trim().replace(/[，,\s]/g, '');
            
            // 验证是否为有效的大写金额
            if (!this.isValidUppercaseAmount(cleanAmount)) {
                return { success: false, error: '请输入有效的大写金额格式' };
            }

            const result = this.convertToLowercase(cleanAmount);
            return { success: true, data: result };
        } catch (error) {
            return { success: false, error: '转换失败：' + error.message };
        }
    }

    static isValidUppercaseAmount(amount) {
        const validChars = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖',
                           '拾', '佰', '仟', '万', '亿', '元', '角', '分', '整'];
        
        for (let char of amount) {
            if (!validChars.includes(char)) {
                return false;
            }
        }
        
        return amount.includes('元');
    }

    static convertToLowercase(upperAmount) {
        let result = 0;
        
        // 分离元和角分部分
        const parts = upperAmount.split('元');
        const integerPart = parts[0];
        const decimalPart = parts[1] || '';
        
        // 转换整数部分
        if (integerPart && integerPart !== '零') {
            result += this.parseIntegerPart(integerPart);
        }
        
        // 转换小数部分
        if (decimalPart && decimalPart !== '整') {
            result += this.parseDecimalPart(decimalPart);
        }
        
        return result.toFixed(2);
    }

    static parseIntegerPart(integerPart) {
        const reverseDigitMap = {
            '零': 0, '壹': 1, '贰': 2, '叁': 3, '肆': 4,
            '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖': 9
        };
        
        let result = 0;
        
        // 处理万亿
        if (integerPart.includes('万亿')) {
            const parts = integerPart.split('万亿');
            result += this.parseSmallGroup(parts[0], reverseDigitMap) * 1000000000000;
            integerPart = parts[1];
        }
        
        // 处理亿
        if (integerPart.includes('亿')) {
            const parts = integerPart.split('亿');
            result += this.parseSmallGroup(parts[0], reverseDigitMap) * 100000000;
            integerPart = parts[1];
        }
        
        // 处理万
        if (integerPart.includes('万')) {
            const parts = integerPart.split('万');
            result += this.parseSmallGroup(parts[0], reverseDigitMap) * 10000;
            integerPart = parts[1];
        }
        
        // 处理剩余部分
        if (integerPart) {
            result += this.parseSmallGroup(integerPart, reverseDigitMap);
        }
        
        return result;
    }

    static parseSmallGroup(group, reverseDigitMap) {
        if (!group || group === '零') return 0;
        
        let result = 0;
        let currentNum = 0;
        
        for (let i = 0; i < group.length; i++) {
            const char = group[i];
            
            if (reverseDigitMap.hasOwnProperty(char)) {
                currentNum = reverseDigitMap[char];
            } else if (char === '拾') {
                if (currentNum === 0) currentNum = 1;
                result += currentNum * 10;
                currentNum = 0;
            } else if (char === '佰') {
                result += currentNum * 100;
                currentNum = 0;
            } else if (char === '仟') {
                result += currentNum * 1000;
                currentNum = 0;
            }
        }
        
        result += currentNum;
        return result;
    }

    static parseDecimalPart(decimalPart) {
        const reverseDigitMap = {
            '零': 0, '壹': 1, '贰': 2, '叁': 3, '肆': 4,
            '伍': 5, '陆': 6, '柒': 7, '捌': 8, '玖': 9
        };
        
        let result = 0;
        
        // 处理角
        const jiaoMatch = decimalPart.match(/([零壹贰叁肆伍陆柒捌玖])角/);
        if (jiaoMatch) {
            result += reverseDigitMap[jiaoMatch[1]] * 0.1;
        }
        
        // 处理分
        const fenMatch = decimalPart.match(/([零壹贰叁肆伍陆柒捌玖])分/);
        if (fenMatch) {
            result += reverseDigitMap[fenMatch[1]] * 0.01;
        }
        
        return result;
    }
}